const http = require("http");
const fs = require("fs");
const path = require("path");
const url = require("url");
const common = require("./module/common");

http.createServer(function(req,res){

    //1、获取网址
    let pathName = url.parse(req.url).pathname;
    console.log(req.url);
    pathName = pathName == "/" ? "/index.html" : pathName;
    //获取后缀名
    let extname = path.extname(pathName);
    //2、通过fs模块读取文件 
    if(pathName != "/favicon.ico"){
        fs.readFile("./static"+pathName,(err,data)=>{
            if(err){
                res.writeHead(404,{'Content-Type':'text/html;charset="utf-8"'});
                res.end("404这个页面不存在");
            }
            let mime = common.getMime(extname);
            res.writeHead(200, {'Content-Type': ''+mime+';charset="utf-8"'}); 
            res.end(data); 
        })
    }
}).listen(3000);